package com.cxm.qyyz.utils;

import android.graphics.Rect;
import android.util.TypedValue;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class ListSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private int spaceDp;
    private boolean begin;
    private boolean end;
    private int lastItemCount;

    public ListSpaceItemDecoration(int spaceDp) {
        this.spaceDp = spaceDp;
    }

    public ListSpaceItemDecoration setShowSpace(boolean begin,boolean end){
        this.begin = begin;
        this.end = end;
        return this;
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        int space = (int) (TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,spaceDp,parent.getResources().getDisplayMetrics()) + 0.5f);
        if (parent.getLayoutManager() instanceof LinearLayoutManager) {
            LinearLayoutManager layoutManager = (LinearLayoutManager) parent.getLayoutManager();
            RecyclerView.Adapter adapter = parent.getAdapter();
            if (layoutManager != null && adapter != null) {
                int orientation = layoutManager.getOrientation();
                int itemCount = adapter.getItemCount();
                if (orientation == RecyclerView.VERTICAL) {
                    if (layoutManager.findViewByPosition(0) == view) {
                        //第一行
                        if (begin) {
                            outRect.set(0,space,0,space);
                        }else {
                            outRect.set(0,0,0,space);
                        }
                    }else if (layoutManager.findViewByPosition(itemCount - 1) == view){
                        //最后一行
                        if (end){
                            outRect.set(0,0,0,space);
                        }else {
                            outRect.set(0,0,0,0);
                        }
                    }else {
                        if (lastItemCount == itemCount) {
                            outRect.set(0,0,0,space);
                        }else {
                            //分页加载的第一条
                            outRect.set(0,space,0,space);
                        }
                    }
                } else {
                    if (layoutManager.findViewByPosition(0) == view) {
                        //第一行
                        if (begin) {
                            outRect.set(space,0,space,0);
                        }else {
                            outRect.set(0,0,space,0);
                        }
                    }else if (layoutManager.findViewByPosition(itemCount - 1) == view){
                        //最后一行
                        if (end) {
                            outRect.set(0,0,space,0);
                        }else {
                            outRect.set(0,0,0,0);
                        }
                    }else {
                        outRect.set(0,0,space,0);
                    }
                }
                lastItemCount = itemCount;
            }
        }
    }
}